package com.jt.auth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
/*
登陆时用户信息的获取和封装会在此对象进行实现
在页面上点击登录按钮时,会调用这个对象的loadUserByUsername方法
页面上输入的用户名会传给这个方法的参数
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    //UserDetails封装用户信息(认证和权限信息)
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            //1.基于用户名查询用户信息
            //2.对用户信息进行封装
        String encondedPassword = passwordEncoder.encode("123456");
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(
                "sys:res:create","sys:res:retrieve");
        return new User(username,encondedPassword,authorities);
    }
}
